package com.lcb.jmm.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/****
 * @Author:刘长柏
 * @Date: 2021-01-14 10:31
 * @Description: 定时线程池
 *****/
public class DefaultSchedulePoolTest {

    public static void main(String[] args) {

        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);

        executorService.schedule(new Runnable() {
            public void run() {
                System.out.println("哈哈哈");
            }
        }, 1, TimeUnit.SECONDS);

        //执行周期固定的
        executorService.scheduleAtFixedRate(new Runnable() {
            public void run() {
                System.out.println("定时执行");
            }
        }, 1, 3, TimeUnit.SECONDS);

        //执行周期不固定的,会等到执行任务完毕
        executorService.scheduleWithFixedDelay(new Runnable() {
            public void run() {
                System.out.println("定时执行");
            }
        }, 1, 3, TimeUnit.SECONDS);

    }
}
